﻿Imports System.Linq
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Columns


Public Class fManutencaoProjectosProdutos
    Public ano As Integer
    Public db As New lqProjectosDataContext


    Private Sub ComboBoxEdit1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxEdit1.SelectedIndexChanged
        
        If ComboBoxEdit1.SelectedItem <> "" Then
            ano = ComboBoxEdit1.SelectedItem.ToString

            TblProjectosProdutoTableAdapter.Fill(DsProjectos.tblProjectosProduto, ano, GlobalVariables.Empresa)
        End If
    End Sub

    Private Sub GridView1_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles GridView1.CellValueChanged
        Dim view As GridView = TryCast(sender, GridView)
        If view.IsNewItemRow(e.RowHandle) Then
            If e.Column.Name = colCodeProduto.Name Then
                Dim DescProd = (From c In db.tblItemProjectos _
                                 Where c.No_ = e.Value.ToString AndAlso c.Company = GlobalVariables.Empresa _
                                 Select c).SingleOrDefault

                GridView1.SetFocusedRowCellValue(colExtendedDescription, DescProd.Extended_Description.ToString)
                Try
                    Dim NomeGestor = (From c In db.tblUserProjectos _
                                Where c.CodGestProd = GridView1.GetFocusedRowCellValue(colun).ToString _
                                Select c).SingleOrDefault


                    GridView1.SetFocusedRowCellValue(colGESTOR, NomeGestor.PrimeiroNome.ToString & " " & NomeGestor.UltimoNome.ToString)

                Catch ex As Exception
                End Try
            End If

            If e.Column.Name = colCodeProjecto.Name Then
                Dim DescProjecto = (From c In db.tblProjectos _
                                 Where c.Code = e.Value.ToString AndAlso c.Company = GlobalVariables.Empresa _
                                 Select c).SingleOrDefault

                GridView1.SetFocusedRowCellValue(colDescricao, DescProjecto.Descricao.ToString)
                GridView1.SetFocusedRowCellValue(colun, DescProjecto.un.ToString)

            End If
        Else

        End If

    End Sub

    
    Private Sub GridView1_ValidateRow(sender As Object, e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs) Handles GridView1.ValidateRow
        Dim view As GridView = TryCast(sender, GridView)
        If view.IsNewItemRow(e.RowHandle) Then
            Dim columnProj As GridColumn = view.Columns("Code Projecto")
            Dim valProj As Object = view.GetRowCellValue(e.RowHandle, columnProj)
            If valProj Is Nothing OrElse valProj.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Código de Projecto é obrigatório."
                view.SetColumnError(columnProj, "Campo não pode estar Vazio")
            End If

            Dim columnProd As GridColumn = view.Columns("Code Produto")
            Dim valProd As Object = view.GetRowCellValue(e.RowHandle, columnProd)
            If valProd Is Nothing OrElse valProd.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Código de Produto é obrigatório."
                view.SetColumnError(columnProd, "Campo não pode estar Vazio")
            End If
            

            Dim verifica As Integer = -1
            If e.Valid.ToString = True Then
                Dim verificar = (From c In db.tblProjectosProdutos _
                              Where c.ano = ComboBoxEdit1.Text AndAlso c.Code_Produto = GridView1.GetFocusedRowCellValue(colCodeProduto).ToString AndAlso c.Code_Projecto = GridView1.GetFocusedRowCellValue(colCodeProjecto).ToString _
                              Select c.Code_Produto).Count
                verifica = verificar
            End If
            If e.Valid.ToString = True And verifica = 0 Then
                TblProjectosProdutoTableAdapter.InsertQuery(ComboBoxEdit1.Text, GridView1.GetFocusedRowCellValue(colCodeProduto).ToString, GridView1.GetFocusedRowCellValue(colCodeProjecto).ToString, GlobalVariables.Empresa)
            ElseIf e.Valid.ToString = True And verifica > 0 Then
                e.Valid = False
                e.ErrorText = "Linha já existe"
                view.SetColumnError(colCodeProduto, "Linha já existe.")

            End If

        End If
    End Sub

    Private Sub fManutencaoProjectosProdutos_Load(sender As Object, e As EventArgs) Handles Me.Load
        

        Dim ListaAno = (From c In db.tblProjectosProdutos Group By c.ano Into Group _
                      Select ano)

        For Each anoins In ListaAno
            ComboBoxEdit1.Properties.Items.Add(anoins.ToString)
        Next
        ComboBoxEdit1.SelectedIndex = ListaAno.Count

        TblProjectosProdutoTableAdapter.Fill(DsProjectos.tblProjectosProduto, ComboBoxEdit1.Text, GlobalVariables.Empresa)

        TblItemTableAdapter.Fill(DsProjectos.tblItem, GlobalVariables.Empresa)

        TblProjectosTableAdapter.Fill(DsProjectos.tblProjectos, ComboBoxEdit1.Text, GlobalVariables.Empresa, GlobalVariables.Un)

        GridView1.BestFitColumns()
    End Sub

    Private Sub GridControl1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles GridControl1.PreviewKeyDown
        Dim linha As Integer = GridView1.FocusedRowHandle
        If e.KeyCode = Keys.Delete Then
            MessageBox.Show("Deseja eleminar o produto " & GridView1.GetFocusedRowCellValue(colCodeProduto) & " do projecto " & GridView1.GetFocusedRowCellValue(colDescricao) & "?", "Deseja eliminar a linha?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If DialogResult.Yes Then

                Dim DeleteLinha = (From c In db.tblProjectosProdutos _
                                   Where c.id = GridView1.GetFocusedRowCellValue(colid).ToString _
                                   Select c).SingleOrDefault
                db.tblProjectosProdutos.DeleteOnSubmit(DeleteLinha)
                db.SubmitChanges()
                GridView1.DeleteRow(GridView1.FocusedRowHandle)
                
            End If
        End If
    End Sub
End Class